﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;

public partial class admin_roleMenu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    //所有角色
    public void jsSelect() { 
        //提取除qing所有的角色
        OleDbConnection conn = DB.conn();
        if (conn != null) {
            conn.Open();

            string sql = "select id,roleName from roles where roleName<>'qiang' order by roleOrder";
            OleDbCommand comm = new OleDbCommand();
            comm.Connection = conn;
            comm.CommandText = sql;

            OleDbDataAdapter da=new OleDbDataAdapter();
            da.SelectCommand=comm;

            DataTable dt=new DataTable();
            da.Fill(dt);

            StringBuilder sb = new StringBuilder();
            if (dt != null && dt.Rows.Count>0) {
                for (int i = 0; i < dt.Rows.Count;i++ )
                {
                    sb.Append("<option value='" + dt.Rows[i]["id"].ToString() + "'");
                    if (Request["roleid"] != null && Request["roleid"].ToString().Trim().Equals(dt.Rows[i]["id"].ToString()))
                    {
                        sb.Append(" selected ");
                    }
                    sb.Append(" >" + dt.Rows[i]["roleName"].ToString() + "</option>");
                }
            }
            Response.Write(sb.ToString());
        }
    }
    //当前角色权限
    public void jsMenu() {
        if (Request["roleid"] != null && Request["roleid"].ToString().Trim().Length > 0 && !"0".Equals(Request["roleid"].ToString().Trim()))
        {
            string roleid=Request["roleid"].ToString().Trim();
            StringBuilder sb = new StringBuilder();
            sb.Append("<input type=\"hidden\" name=\"roleId\" value=\""+roleid+"\">");
            sb.Append("<script language=\"javascript\">");
            sb.Append("var tree = new MzTreeView(\"tree\");");
            sb.Append("tree.setIconPath(\"TreeView/\");");
            sb.Append("tree.nodes[\"0_1\"] = \"text:系统菜单;\";");
            if (Session["admin"] != null)
            {
                OleDbConnection conn = DB.conn();
                OleDbCommand comm=null;
                try
                {
                    if (conn != null)
                    {
                        conn.Open();
                        String sql = "";
                        comm = new OleDbCommand();
                        comm.Connection = conn;
                        OleDbDataAdapter da = new OleDbDataAdapter();
                        DataTable dt = new DataTable();
                        String par, cur;
                        Object count;

                        //超管显示所有
                        if (Session["admin"].ToString().Equals("qiang"))
                        {
                            sql = "select * from sys_menu";
                        }
                        else
                        {
                            sql = "select sysm.id as id,par_id,menuName from sys_menu as sysm left join role_menu as rolem on sysm.id=rolem.menuId where rolem.roleId=" +Session["roleId"];
                        } 
                        comm.CommandText = sql;
                        da.SelectCommand = comm;
                        da.Fill(dt);
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                par = "1";
                                cur = "";
                                if (dt.Rows[i]["par_id"].ToString().Length > 0)
                                {
                                    par = "0" + dt.Rows[i]["par_id"].ToString();
                                }
                                cur = "0" + dt.Rows[i]["id"].ToString();
                                sb.Append("tree.nodes[\"" + par + "_" + cur + "\"] = \"text:<input id=" + par + "_" + cur + " type=checkbox name=menus value=" + dt.Rows[i]["id"].ToString());
                                count = DB.getObject("select count(*) from role_menu where roleId=" + roleid + " and menuId=" + dt.Rows[i]["id"].ToString());
                                if (count != null && Int32.Parse(count.ToString()) > 0)
                                {
                                    sb.Append(" checked ");
                                }
                                sb.Append(">" + dt.Rows[i]["menuName"].ToString() + ";method:choice('" + par + "_" + cur + "')\";");
                            }

                        }
                    }
                }
                catch (Exception error)
                {
                    Response.Write(error.ToString());
                }
                finally {
                    if (comm != null) {
                        comm.Dispose();
                    }
                    conn.Close();
                }
            }
            sb.Append("document.write(tree.toString());");
            sb.Append("tree.expandAll();");
            sb.Append("</script>");
            Response.Write(sb.ToString());
        }
    }

    //提交处理
    public void roleMenuDo(object sender, EventArgs e)
    {
        if (Request["roleid"] != null && Request["roleid"].ToString().Trim().Length > 0 && !"0".Equals(Request["roleid"].ToString().Trim()))
        {
            string roleid = Request["roleid"].ToString().Trim();
            OleDbConnection conn = DB.conn();
            OleDbCommand comm = null;
            try
            {
                if (conn != null)
                {
                    conn.Open();
                    //先删除原有权限
                    comm = new OleDbCommand();
                    comm.Connection = conn;
                    comm.CommandText = "delete from role_menu where roleId="+roleid;
                    comm.ExecuteNonQuery();

                    //添加新权限
                    if (Request["menus"] != null && Request["menus"].ToString().Trim().Length > 0) {
                        string[] menuArr = Request["menus"].ToString().Trim().Split(',');
                        if (menuArr != null && menuArr.Length > 0) {
                            for (int i = 0; i < menuArr.Length; i++)
                            {
                                RoleMenuDao.add(roleid,menuArr[i]);
                            }
                        }
                    }

                    Response.Write("<script>alert('提交成功！');</script>");
                }
            }
            catch (Exception error)
            {
                Response.Write(error.ToString());
            }
            finally {
                if (comm != null) {
                    comm.Dispose();
                }
                conn.Close();
            }
         }
    }
}
